home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-12-04 | 11.6 KB | 563 lines | [TEXT/MMCC] |
-
- //• C translation from Pascal source file: LightSpeed.p
- //• by Kenneth A. Long, at itty bitty bytes(tm), on 19 November 1994.
- //• Original Pascal by Jonathan Birge.
-
- //• LightSpeed.c
-
- #include <Sound.h>
- #include <math.h>
-
- enum {
- // MBarHeight = 0xBAA, //• Address of menubar height.
- HUDColor = blackColor,
- IndexColor = greenColor,
- starNumber = 40, //• Number of stars on the screen at one time.
- photonSnd = 9000,
- engineSnd = 9001
- };
-
- typedef short *IntPtr;
- typedef struct {
- double h, v;
- double distance;
- } StarRecord;
-
- enum {
- PhotonNum = 18,
- maxDist = 24
- };
-
- typedef struct {
- float h;
- float v;
- double pSize;
- } PhotonVector;
-
- RGBColor starColor;
- short star, i, t;
- short hPos, vPos;
- PhotonVector photon[PhotonNum];
- PhotonVector oldPhoton;
- short photonCount;
- double oldpsize;
- Boolean keepRunning, past, offscreen;
- double shipSpeed, dist;
- Rect starRect;
- short windowWidth, windowHight, midH, midV;
- Point mouseLoc, oldmouseLoc;
- short hOffset, vOffset;
- Rect hRect, vRect;
- Handle engineSound, photonSound;
- SndChannelPtr soundChannel;
- SndCommand stopCommand;
- OSErr err;
-
- typedef StarRecord StarList[starNumber];
-
- StarList stars;
- WindowPtr starsWindow;
- Handle dataHandle;
- GrafPtr currentPort;
- Rect starswindowRect;
- RgnHandle GrayRgn;
- short *mBarHeightPtr;
- short oldMBarHeight;
- RgnHandle mBarRgn;
- short colorList[3];
- EventRecord theEvent;
-
- #include <SANE.h>
-
- //• Random number between -(high) and (high).
- double Randomize (short high)
- {
- long rawResult;
-
- rawResult = Random ();
- return (((rawResult * high) / 32768));
- }
-
- //• Pos number between low and high.
- double RandMinMax (double low, double high)
- {
- long rawResult;
-
- rawResult = Random ();
- return (rawResult * (high - low) / 32768) + low;
- }
-
- //• Random integer between 1 and high.
- short IntRandomize (short range)
- {
- long rawResult;
-
- rawResult = Random ();
- return ((rawResult * range) / 32768);
- }
-
- short Sgn (short number)
- {
- if (number > 0)
- return (1);
- else
- if (number < 0)
- return (-1);
- else
- return (0);
- }
-
- short noSgn (short number)
- {
- return (0);
- if (number > 0)
- return (1);
- if (number < 0)
- return (-1);
- }
-
- void HideMenuBar ()
- {
- Rect mBarRect;
-
- oldMBarHeight = *mBarHeightPtr;
-
- //• Make the Menu Bar's height zero.
- *mBarHeightPtr = 0;
- mBarRect = qd.screenBits.bounds;
- mBarRect.bottom = mBarRect.top + oldMBarHeight;
- mBarRgn = NewRgn ();
- RectRgn (mBarRgn, &mBarRect);
- UnionRgn (GrayRgn, mBarRgn, GrayRgn); //• Tell the desktop it.
-
- //• covers the menu bar.
- PaintOne (0L, mBarRgn); //• Redraw desktop.
- }
-
- void ShowMenuBar ()
- {
- *mBarHeightPtr = oldMBarHeight;
-
- //• Remove the menu bar from the desktop.
- DiffRgn (GrayRgn, mBarRgn, GrayRgn);
- DisposeRgn (mBarRgn);
- }
-
- void CenterOrigin ()
- {
- short centerX, centerY;
-
- centerX = - (qd.screenBits.bounds.right / 2);
- // centerX = - (abs (currentPort->portRect.right / 2));
- centerY = - (qd.screenBits.bounds.bottom / 2);
- // centerY = - (abs (currentPort->portRect.bottom / 2));
- SetOrigin (centerX, centerY);
- }
-
- void ClearScrn ()
- {
- short oldPenState, oldBkColor;
- GrafPtr winMgrPort;
- Rect menuRect;
-
- oldPenState = currentPort->pnMode;
- GetWMgrPort (&winMgrPort);
- oldBkColor = winMgrPort->bkColor;
- SetPort (winMgrPort);
- BackColor (blackColor);
- SetRect (&menuRect, 0, 0, qd.screenBits.bounds.right, 20);
- EraseRect (&winMgrPort->portRect);
- BackColor (oldBkColor);
- SetPort (currentPort);
- }
-
- void MakeRect (double h, double v, double distance, Rect *theRect)
- {
- short Size;
-
- Size = ceil (maxDist / (distance + 0.01));
- theRect->left = ceil (h);
- theRect->top = ceil (v);
- theRect->right = theRect->left + Size;
- theRect->bottom = theRect->top + Size;
- OffsetRect (theRect, - (Size / 2), - (Size / 2));
-
- }
-
- void LoadStars ()
- {
- short star;
- Rect starRect;
-
- for (star = 1; star <= starNumber; star++)
- {
- stars[star].h = Randomize (midH);
- stars[star].v = Randomize (midV);
- stars[star].distance = RandMinMax (3, maxDist);
-
- MakeRect (stars[star].h,
- stars[star].v,
- stars[star].distance, &starRect);
-
- InvertOval (&starRect);
- }
- }
-
- void DoMouseDown ()
- {
- if (photonCount < 10)
- {
- err = SndDoImmediate (soundChannel, &stopCommand);
- err = SndPlay (soundChannel, photonSound, true);
- photon[photonCount + 1].h = -midH;
- photon[photonCount + 1].v = midV;
- photon[photonCount + 2].h = midH;
- photon[photonCount + 2].v = midV;
- photon[photonCount + 1].pSize = 48;
- photon[photonCount + 2].pSize = 48;
- photonCount = photonCount + 2;
- }
- }
-
- void DoKeyDown (EventRecord *theEvent)
- {
- short chCode;
- chCode = theEvent->message & charCodeMask;
-
- switch (chCode)
- {
- case '+':
- shipSpeed = shipSpeed + 0.1;
- break;
-
- case '-':
- shipSpeed = shipSpeed - 0.1;
- break;
-
- case 'q':
- case 'Q':
- keepRunning = false;
- break;
-
- case '0':
- shipSpeed = 0;
- break;
-
- case ' ':
- DoMouseDown ();
- break;
-
- case '4':
- hPos = hPos - 5;
- break;
-
- case '6':
- hPos = hPos + 5;
- break;
-
- case '8':
- vPos = vPos + 5;
- break;
-
- case '5':
- vPos = vPos - 5;
- break;
-
- case '1':
- {
- vPos = 0;
- hPos = 0;
- }
- break;
- }
- }
-
- void DrawPhoton (float h, float v, double pSize)
- {
- short t, offset, offset2;
- short h2, v2;
- Rect photonRect;
-
- colorList[1] = blueColor;
- colorList[0] = magentaColor;
- colorList[2] = cyanColor;
-
- h2 = ceil (h);
- v2 = ceil (v);
- for (t = 0; t < 4; t++)
- {
- ForeColor (colorList[IntRandomize (3)]);
- offset = ceil (sin (pSize + t) * pSize);
- offset2 = ceil (sin ((pSize + t) * 2) * pSize);
-
- MoveTo (h2 - offset, v2 - offset2);
- LineTo (h2 + offset, v2 + offset2);
- }
- }
-
- void MainLoop ()
- {
- CenterOrigin ();
- BackColor (blackColor);
-
- windowWidth = qd.screenBits.bounds.right;
- // windowWidth = (currentPort->portRect.right - currentPort->portRect.left);
- windowHight = qd.screenBits.bounds.bottom;
- // windowHight = (currentPort->portRect.bottom - currentPort->portRect.top);
-
- midH = windowWidth / 2;
- midV = windowHight / 2;
- LoadStars ();
- ForeColor (HUDColor);
- PenNormal ();
- PenPat (&qd.black);
- engineSound = GetResource ('snd ', engineSnd);
- photonSound = GetResource ('snd ', photonSnd);
-
- stopCommand.cmd = quietCmd;
- stopCommand.param1 = 0;
- stopCommand.param2 = 0;
-
- soundChannel = 0L;
- err = SndNewChannel (&soundChannel, sampledSynth, initMono, 0L);
-
- keepRunning = true;
- photonCount = 0;
- shipSpeed = 0;
- hPos = 0;
- vPos = 0;
-
- starColor.red = 0xffff;
- starColor.green = 0xffff;
- starColor.blue = 0xbbbb;
-
- SetEventMask (mDownMask + keyDownMask + autoKeyMask);
- GetMouse (&oldmouseLoc);
- while (keepRunning)
- {
- if (GetNextEvent (everyEvent, &theEvent))
- switch (theEvent.what)
- {
- case mouseDown:
- DoMouseDown ();
- break;
-
- case keyDown:
- case autoKey:
- DoKeyDown (&theEvent);
- break;
-
- default:
- break;
- }
- //• Moves the direction indicators.
- GetMouse (&mouseLoc);
- if (! EqualPt (mouseLoc, oldmouseLoc))
- {
- hPos = hPos + (mouseLoc.h - oldmouseLoc.h);
- vPos = vPos + (mouseLoc.v - oldmouseLoc.v);
- oldmouseLoc = mouseLoc;
- }
- //• Draws the index bars.
- ForeColor (IndexColor); //• Green color.
- PenMode (srcCopy);
-
- if (abs (hPos) > (midH - 2))
- hPos = Sgn (hPos) * (midH - 2);
-
- if (abs (vPos) > (midV - 2))
- vPos = Sgn (vPos) * (midV - 2);
-
- vRect.left = - (midH);
- vRect.right = - (midH) + 8;
- vRect.top = vPos;
- vRect.bottom = vPos + 2;
- PaintRect (&vRect);
-
- hRect.bottom = midV;
- hRect.top = midV - 8;
- hRect.left = hPos;
- hRect.right = hPos + 2;
- PaintRect (&hRect);
-
- hOffset = -hPos;
- vOffset = vPos;
- PenMode (srcCopy);
-
- //• If there are any photons...
- if (photonCount > 0)
- {
- PenMode (srcCopy);
- for (i = 1; i <= photonCount; i++)
- DrawPhoton (photon[i].h, //• Horizontal.
- photon[i].v, //• Vertical.
- photon[i].pSize); //• Size.
- }
-
- //• Calculate new star position.
- //• If star out of window, reset it.
- for (star = 1; star <= starNumber; star++)
- {
- MakeRect (stars[star].h, stars[star].v, stars[star].distance, &starRect);
- EraseOval (&starRect);
-
- if ((shipSpeed < 0) && (stars[star].distance >= maxDist))
- past = true;
-
- if ((shipSpeed > 0) && (stars[star].distance <= 0))
- past = true;
-
- if ((/*abs*/ (stars[star].v) > midV) || (/*abs*/ (stars[star].h) > midH))
- offscreen = true;
-
- //• Create a new star.
- if ((past || offscreen))
- {
- past = false;
- offscreen = false;
- if (shipSpeed >= 0)
- {
- stars[star].v = Randomize (midV - 10);
- stars[star].h = Randomize (midH - 10);
- stars[star].distance = maxDist;
- }
- else //• shipSpeed < 0.
- switch (IntRandomize (3))
- {
- case 1:
- {
- if (IntRandomize (2) == 1)
- stars[star].v = midV;
- else
- stars[star].v = -midV;
-
- stars[star].h = Randomize (midH);
- stars[star].distance = RandMinMax (2, maxDist - 1);
- }
- break;
-
- case 2:
- {
- stars[star].v = Randomize (midV);
- if (IntRandomize (2) == 1)
- stars[star].h = midH;
- else
- stars[star].h = -midH;
-
- stars[star].distance = RandMinMax (2, maxDist - 1);
- }
- break;
- }
- } //• new star.
- else
- {
- dist = 6 * stars[star].distance;
-
- //• How much distance affects apparent speed.
- stars[star].h = stars[star].h * (shipSpeed + dist) / dist +
- (hOffset / 8);
-
- stars[star].v = stars[star].v * (shipSpeed + dist) / dist +
- (vOffset / 6);
-
- stars[star].distance = stars[star].distance - (shipSpeed / 6);
- }
-
- MakeRect (stars[star].h, stars[star].v, stars[star].distance, &starRect);
-
- RGBForeColor (&starColor);
- PaintOval (&starRect);
- }
-
- //• Draw the "heads up" display (sight).
- //• Moved to here so stars and photons don't erase it.
- PenPat (&qd.gray);
- ForeColor (yellowColor);
- i = 0;
- while (i < midH)
- {
- i = i + 50;
- MoveTo (i, -5);
- Line (0, 10);
- MoveTo (-i, -5);
- Line (0, 10);
- }
- i = 0;
- while (i < midV)
- {
- i = i + 50;
- MoveTo (-5, i);
- Line (10, 0);
- MoveTo (-5, -i);
- Line (10, 0);
- } //• Done drawing sight.
-
- PenNormal ();
-
- //• If there are photons, we want them to disappear
- //• in the distance.
- if (photonCount > 0)
- {
- PenMode (srcBic);
- for (i = 1; i <= photonCount; i++)
- {
- oldPhoton = photon[i];
- photon[i].h = photon[i].h * 0.86 + (hOffset / 8);
- photon[i].v = photon[i].v * 0.86 + (vOffset / 6);
-
- DrawPhoton (oldPhoton.h, oldPhoton.v, oldPhoton.pSize);
-
- oldpsize = photon[i].pSize;
-
- photon[i].pSize = photon[i].pSize * 0.9;
-
- //• Reduced to double and abs removed to make
- //• it work at half the value.
- //• This part kills the farthest photon.
- if ((oldpsize - photon[i].pSize) < 0.09)
- {
- for (t = i; t <= (photonCount - 1); t++)
- photon[t] = photon[t + 1];
- photonCount = photonCount - 1;
- }
- }
- }
- EraseRect (&hRect);
- EraseRect (&vRect);
- }
- ReleaseResource (photonSound);
- ReleaseResource (engineSound);
- err = SndDisposeChannel (soundChannel, true);
- SetEventMask (everyEvent);
- }
-
- main ()
- {
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
-
- InitCursor();
-
- MaxApplZone ();
-
- GetDateTime ((unsigned long *) qd.randSeed);
-
- mBarHeightPtr = (short *) 0x0BAA;
- GrayRgn = GetGrayRgn ();
- HideMenuBar ();
- starsWindow = NewCWindow (0L, &qd.screenBits.bounds, "\pLightSpeed", true, noGrowDocProc, (WindowPtr)-1L, false, (long) dataHandle);
- SetPort (starsWindow);
-
- GetPort (¤tPort);
- ClearScrn ();
- HideCursor ();
- MainLoop ();
- ShowCursor ();
- ShowMenuBar ();
- FlushEvents (mDownMask, 0); //• Clear Event Queue of all mouseDown events.
- }